package com.ccxc.question.exam.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ccxc.question.exam.dto.AreaDTO;
import com.ccxc.question.exam.dto.AreaDTO2;
import com.ccxc.question.exam.dto.QuestionDTO;
import com.ccxc.question.exam.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/v1/open/question/manager")
@Api("试题操作相关API")
@Slf4j
public class ExamController {
    @Autowired
    private QuestionService questionService;

    /**
     * @DEscription 功能说明 ：添加试题API
     * 参数说明：
     */
    @PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "添加试题", notes = "添加试题<br><br><b>@author 郑尚锟</b>")
    public QuestionDTO add(@ApiParam(value = "题干及选项", required = true) @RequestBody QuestionDTO questionDto) {
        log.debug("添加试题内容：{}", questionDto);
        questionService.add(questionDto);
        return questionDto;
    }

    /**
     * @DEscription 功能说明 ：查询试题API
     * 参数说明：
     */
    @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "根据ID查询试题及选项列表", notes = "根据ID查询试题及选项列表<br><br><b>@author 郑尚锟</b>")
    public QuestionDTO get(@ApiParam(value = "题干ID", required = true) @PathVariable String id) {
        log.debug("获取试题内容ID：{}", id);
        return questionService.findById(id);
    }

    /**
     * @DEscription 功能说明：修改试题API
     * 参数说明：
     */
    @PutMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "PUT", value = "修改试题", notes = "修改试题<br><br><b>@author 郑尚锟</b>")
    public QuestionDTO update(
            @ApiParam(value = "题干ID", required = true) @PathVariable String id,
            @ApiParam(value = "题干ID及选项ID是必须的", required = true) @RequestBody QuestionDTO questionDto
    ) {
        questionDto.setId(id);
        log.debug("修改试题内容：{}", questionDto);
        questionService.update(questionDto);
        return questionDto;
    }

    /**
     * @DEscription 功能说明 ：删除试题
     * 参数说明：
     */
    @DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "DELETE", value = "删除试题及对应的选项", notes = "删除试题及对应的选项<br><br><b>@author 郑尚锟</b>")
    public void delete(@ApiParam(value = "题干ID", required = true) @PathVariable String id) {
        log.debug("删除试题ID：{}", id);
        questionService.delete(id);
    }

    /**
     * @DEscription 功能说明：随机抽题
     * 参数说明：
     */
    @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "随机抽取一道题", notes = "随机抽取一道题<br><br><b>@author 郑尚锟</b>")
    public QuestionDTO look() {
        return questionService.look();
    }

    /**
     * @DEscription 功能说明：根据选择的知识领域抽题
     * 参数说明：
     */
    @PostMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "根据选择的知识领域抽取题目", notes = "根据选择的知识领域抽取题目<br><br><b>@author 郑尚锟</b>")
    public QuestionDTO lookarea(@ApiParam(value = "知识领域", required = true) @RequestBody AreaDTO areaDTO) {
        log.debug("获取试题内容ID：{}", areaDTO);
        return questionService.findByarea(areaDTO);
    }

    /**
     * @DEscription 功能说明：返回全部的知识领域
     * 参数说明：
     */
    @GetMapping(value = "/lookarea", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查看所以知识领域", notes = "随机抽取一道题<br><br><b>@author 郑尚锟</b>")
    public List<AreaDTO2> area() {
        return questionService.area();
    }
}
